// pages/management/management.js
var http = require('../../utils/rquest.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    super: false,
    userList: [],
    activeName: 'role_member',
    show: false,
    radio: 'role_member1',
    currentid: '',
    role: true,
    active: 0,
    currentobj: {
      role_member: 1,
      role_admin: 1,
    },
    userdata: [],
    managedata: [],
    userTotal: 0,
    managetotal: 0
  },
  //tab切换点击事件
  onChange(event) {
    let Name = ''
    switch (event.detail.title) {
      case '用户列表':
        Name = "role_member"
        break
      case '管理员列表':
        Name = "role_admin"
        break
    }
    this.setData({
      activeName: Name,
      active: event.detail.index
    });
    this.getUserList()
  },

  //获取用户列表
  getUserList(options) {
    wx.showLoading({
      title: '加载中',
      icon: 'loading'
    })
    http.userList({
        current: this.data.currentobj[this.data.activeName],
        roleList: this.data.activeName
      })
      .then(res => {
        let arr = res.data.msg.list
        let role_ = ''
        let uTotal = Math.ceil(res.data.msg.total / res.data.msg.pageSize)
        let mTotal = Math.ceil(res.data.msg.total / res.data.msg.pageSize)


        for (var i = 0; i < arr.length; i++) {
          switch (arr[i].roleList) {
            case "role_member1":
              role_ = '普通'
              break
            case "role_member2":
              role_ = '中级'
              break
            case "role_member3":
              role_ = '高级'
              break
          }
          arr[i].roleList = role_
        }


        if (this.data.activeName === "role_member") {
          this.setData({
            userTotal: uTotal
          })
          ////////////////
          if (this.data.currentobj["role_member"] == 1) {
            //如果是当前页我就给你赋值
            this.setData({
              userdata: arr
            })
          } else {
            if (options == "缓存") {
              this.setData({
                userdata: this.data.userdata
              })
            } else {
              //如果不是当前页我就给你拼接新数据
              this.setData({
                userdata: this.data.userdata.concat(arr)
              })
            }
          }
          /////////////////
        } else if (this.data.activeName === "role_admin") {
          this.setData({
            managetotal: mTotal
          })
          ////////////////
          if (this.data.currentobj["role_admin"] == 1) {
            //如果是当前页我就给你赋值
            this.setData({
              managedata: arr
            })
          } else {
            if (options == "缓存") {
              this.setData({
                managedata: this.data.managedata
              })
            } else {
              //如果不是当前页我就给你拼接新数据
              this.setData({
                managedata: this.data.managedata.concat(arr)
              })
            }
          }
          /////////////////
        }

        wx.hideLoading()
        wx.showToast({
          title: '',
        })
      })
  },
  //单选按钮
  openDrawer(event) {
    this.setData({
      radio: event.detail,
    });
  },

  //删除缓存中数据
  deletedata(val, status) {
    let udata = this.data.userdata;
    let mdata = this.data.managedata;
    if (this.data.activeName == "role_member") {
      let grade = ''
      for (var i = 0; i < udata.length; i++) {
        if (status == '修改') {
          if (udata[i].id == val.id) {
            switch (val.roleList) {
              case "role_member1":
                grade = "普通"
                break;
              case "role_member2":
                grade = "中级"
                break;
              case "role_member3":
                grade = "高级"
                break;
              case "role_admin":
                grade = "管理员"
                break;
            }
            val.roleList = grade
            udata.splice(i, 1, val)
          }
        } else {
          if (udata[i].id === val) {
            udata.splice(i, 1)
          }
        }
      }

      this.setData({
        userdata: udata
      })
    }


    if (this.data.activeName == "role_admin") {
      for (var i = 0; i < mdata.length; i++) {
        if (mdata[i].id === val) {
          mdata.splice(i, 1)
        }
      }

      this.setData({
        managedata: mdata
      })
    }
    this.getUserList("缓存")
  },

  //删除按钮
  delete(e) {
    if (this.data.role) {
      wx.showToast({
        title: '权限不足',
      })
      return
    }
    http.userDelete({
        id: e.currentTarget.dataset.id
      })
      .then(res => {
        wx.showToast({
          title: '删除成功',
          icon: 'succes',
          duration: 1000,
          mask: true
        })
        this.deletedata(e.currentTarget.dataset.id, "删除")
      })
      .catch(err => {})
  },

  //修改权限确定按钮
  confirmBtn() {
    if (this.data.role) {
      wx.showToast({
        title: '权限不足',
      })
      return
    }
    let currentUser = wx.getStorageSync('userMsg')
    let obj = {
      'id': this.data.currentid,
      'roleList': this.data.radio
    }
    http.userEdit('?userId=' + currentUser.id, obj)
      .then(res => {
        if (res.data.status == 'success') {
          wx.showToast({
            title: '修改成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
          this.deletedata(res.data.sysUser, "修改")
          this.onClose()
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        }
      })
      .catch(err => {
      })
  },
  //打开抽屉
  showPopup(e) {
    this.setData({
      show: true,
      currentid: e.currentTarget.dataset.id
    });
  },
  //关闭抽屉
  onClose() {
    this.setData({
      show: false
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getUserList()
    let userMsg = wx.getStorageSync('userMsg')
    if (userMsg.roleList == "role_admin" || userMsg.roleList == "role_superAdmin") {
      this.setData({
        role: false
      })
      if (userMsg.roleList == "role_superAdmin") {
        this.setData({
          super: true
        })
      }
    }else{
      this.setData({
        role: true
      })
    }


  },

  //上一页
  prevent() {
    let keys = "currentobj." + this.data.activeName
    this.setData({
      [keys]: 1
    })
    this.getUserList()
  },
  //下一页
  next() {
    let userlist = this.data.currentobj["role_member"]
    let managelist = this.data.currentobj["role_admin"]
    let keyone = "currentobj.role_member"
    let keytow = "currentobj.role_admin"
    if (this.data.activeName === "role_member" && userlist !== this.data.userTotal) {
      userlist++
      this.setData({
        [keyone]: userlist
      })
      this.getUserList()
    }

    if (this.data.activeName === "role_admin" && managelist !== this.data.managetotal) {
      managelist++
      this.setData({
        [keytow]: managelist
      })
      this.getUserList()
    }

  },
  //监听tabbar点击
  onTabItemTap(item) {
    if (getCurrentPages().length != 0) {
      //刷新当前页面的数据
      getCurrentPages()[getCurrentPages().length - 1].onLoad()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})